from datetime import timedelta

from django.shortcuts import render
import redis
from django.http import HttpResponse
import json

pool = redis.ConnectionPool(host='185.227.152.91', password='hfhdtrDGKGIG', port=6377, db=1, decode_responses=True)
r = redis.Redis(connection_pool=pool)
import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["binancedb"]
mydb_orders = mydb["orders"]

ETCUSDT_last_signal = 0
ETCUSDT_signal_1_count = 0
ETCUSDT_signal_2_count = 0
ETCUSDT_signal_3_count = 0
ETCUSDT_signal_4_count = 0

users = []


def show(request):
    response = HttpResponse()

    ETCUSDT_now_date = r.get('ETCUSDT_now_date')
    ETCUSDT_now_price = r.get('ETCUSDT_now_price')

    ETCUSDT_MA210_1m = r.get('ETCUSDT_MA210_1m')
    ETCUSDT_now_macd_1m = r.get('ETCUSDT_now_macd_1m')
    ETCUSDT_now_macd_15m = r.get('ETCUSDT_now_macd_15m')
    ETCUSDT_klines_change_1m = r.get('ETCUSDT_klines_change_1m')
    ETCUSDT_klines_change_15m = r.get('ETCUSDT_klines_change_15m')

    ETCUSDT_divergence_1m = r.get('ETCUSDT_divergence_1m')
    ETCUSDT_last_time_top_divergence_1m = r.get('ETCUSDT_last_time_top_divergence_1m')
    ETCUSDT_last_time_bottom_divergence_1m = r.get('ETCUSDT_last_time_bottom_divergence_1m')
    ETCUSDT_score_details = r.get('ETCUSDT_score_details')
    ETCUSDT_score = r.get('ETCUSDT_score')
    ETCUSDT_signal = r.get('ETCUSDT_signal')

    # response.write("<script>setTimeout(window.location.reload(),100000)</script>")
    response.write("<p>==========>ETCUSDT<==============</p>")
    response.write("<p>最新日期: " + ETCUSDT_now_date + "</p>")
    response.write("<p>最新成交价格信息: " + ETCUSDT_now_price + "</p>")

    response.write("<p>1分钟均衡线: " + ETCUSDT_MA210_1m + "</p>")
    # response.write("<p>15分钟均衡线: " + r.get('ETCUSDT_MA210_15m') + "</p>")

    response.write("<p>1分钟MACD信息: " + ETCUSDT_now_macd_1m + "</p>")
    response.write("<p>15分钟MACD信息: " + ETCUSDT_now_macd_15m + "</p>")

    response.write("<p>1m转折点 前=>后: " + ETCUSDT_klines_change_1m + "</p>")
    response.write("<p>15m转折点 前=>后: " + ETCUSDT_klines_change_15m + "</p>")

    if not ETCUSDT_divergence_1m is None:
        if int(ETCUSDT_divergence_1m) == 1:
            response.write("<p>背离情况: 顶背离  " + str(r.ttl('ETCUSDT_divergence_1m')) + "s</p>")
        elif int(ETCUSDT_divergence_1m) == -1:
            response.write("<p>背离情况: 底背离  " + str(r.ttl('ETCUSDT_divergence_1m')) + "s</p>")
    else:
        response.write("<p>背离情况: 无</p>")

    if ETCUSDT_last_time_top_divergence_1m is not None:
        response.write("<p>上次1m顶背离时间: " + ETCUSDT_last_time_top_divergence_1m + "</p>")
    if ETCUSDT_last_time_bottom_divergence_1m is not None:
        response.write("<p>上次1m底背离时间: " + ETCUSDT_last_time_bottom_divergence_1m + "</p>")

    response.write("<p>条件成立: " + ETCUSDT_score_details + "</p>")
    response.write("<p>总分: " + ETCUSDT_score + "</p>")

    global ETCUSDT_signal_1_count, ETCUSDT_signal_2_count, ETCUSDT_signal_3_count, ETCUSDT_signal_4_count, ETCUSDT_last_signal

    if int(ETCUSDT_score) >= 2:
        response.write("<p>信号: 做空 平多</p>")

        if ETCUSDT_last_signal != 1:
            ETCUSDT_last_signal = 1
            ETCUSDT_signal_1_count += 1
            ETCUSDT_signal_2_count += 1

    elif int(ETCUSDT_score) >= 1:
        response.write("<p>信号: 平多</p>")

        if ETCUSDT_last_signal != 2:
            ETCUSDT_last_signal = 2
            ETCUSDT_signal_2_count += 1

    elif int(ETCUSDT_score) <= -2:
        response.write("<p>信号: 做多 平空</p>")

        if ETCUSDT_last_signal != 3:
            ETCUSDT_last_signal = 3
            ETCUSDT_signal_3_count += 1
            ETCUSDT_signal_4_count += 1


    elif int(ETCUSDT_score) <= -1:
        response.write("<p>信号: 平空</p>")

        if ETCUSDT_last_signal != 4:
            ETCUSDT_last_signal = 4
            ETCUSDT_signal_4_count += 1
    else:
        response.write("<p>信号: 无</p>")
        ETCUSDT_last_signal = 0

    response.write(
        "<p>出现次数=>[做空: " + str(ETCUSDT_signal_1_count) + "]   [做多: " + str(ETCUSDT_signal_3_count) + "]</p>")

    response.write(
        "<p>空仓止损区间最高: " + str(r.get("ETCUSDT_short_stop_loss")) + "</p>")

    response.write(
        "<p>多仓止损区间最低: " + str(r.get("ETCUSDT_long_stop_loss")) + "</p>")
    return response

